package com.aiju.cash.ui.stockupdate.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.stockupdate.History;
import com.aiju.cash.ui.stockupdate.adapter.HistoryAdapter;
import com.aiju.cash.ui.stockupdate.widget.CustomClearAndSearechEdittext;
import com.aiju.cash.ui.stockupdate.widget.NoScrollListview;
import com.aiju.utils.StringUtil;
import com.aiju.utils.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 流冥
 * @date: 2017-6-30
 * @Description: 商品搜索
 */
public class GoodsSearchActivity extends BaseTitleActivity implements CustomClearAndSearechEdittext.OnClickSearchListener, HistoryAdapter.HistoryListener {
    public static final String EXTRA_KEY_TYPE = "extra_key_type";
    private static final String PRE_SEARCH_HISTORY = "pre_search_history";
    private static final String SEARCH_HISTORY = "search_history";
    NoScrollListview listView;
    HistoryAdapter adapter;
    TextView clear_all;
    LinearLayout layout;
    CustomClearAndSearechEdittext customClearAndSearechEdittext;
    private SharedPreferences mSharePreference;
    Gson gson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_search);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        init();
        updateData();

    }

    void init() {
        initView();
        initTitle();
        initListener();
    }

    void initView() {
        customClearAndSearechEdittext = (CustomClearAndSearechEdittext) findViewById(R.id.search_bar);
        listView = (NoScrollListview) findViewById(R.id.historty);
        clear_all = (TextView) findViewById(R.id.clear_all);
        layout = (LinearLayout) findViewById(R.id.layout_search_goods);
        mSharePreference = getSharedPreferences(PRE_SEARCH_HISTORY, 0);
        gson = new Gson();
    }

    void initTitle() {
        setCenterTitle("商品搜索");
    }

    void initListener() {
        customClearAndSearechEdittext.setOnClickSearchListener(this);
        clear_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanHistory();
            }
        });
    }


    @Override
    public void onClickSearch(String value) {
        if (StringUtil.isBlank(value)) {
            T.ToastShort("请输入商品名称或者条形码");
        } else {
            save(value);
            intentToActivity(value);
        }
    }

    //保存搜索内容到本地
    public void save(String text) {
        List<History> list = new ArrayList<>();
        List<History> oldList = new ArrayList<>();

        list.add(new History(text));

        String oldText = mSharePreference.getString(SEARCH_HISTORY, "");
        if (!StringUtil.isBlank(oldText)) {
            oldList = gson.fromJson(oldText, new TypeToken<List<History>>() {
            }.getType());
        }
        SharedPreferences.Editor myEditor = mSharePreference.edit();
        for (int i = 0; i < oldList.size(); i++) {
            if (text.equals(oldList.get(i).getName())) {
                oldList.remove(i);
                break;
            }
        }
        list.addAll(oldList);
        myEditor.putString(SEARCH_HISTORY, gson.toJson(handlerHistoryList(list)));
        myEditor.commit();
    }


    //清空本地历史
    public void cleanHistory() {
        SharedPreferences.Editor editor = mSharePreference.edit();
        editor.clear();
        editor.commit();
        updateData();
    }

    public void updateData() {
        String history = mSharePreference.getString(SEARCH_HISTORY, "");
        if ("[]".equals(history) || StringUtil.isBlank(history)) {
            layout.setVisibility(View.GONE);
            return;
        } else {
            layout.setVisibility(View.VISIBLE);
        }
        List<History> list = gson.fromJson(history, new TypeToken<List<History>>() {
        }.getType());
        if (adapter == null) {
            adapter = new HistoryAdapter(this, list);
            listView.setAdapter(adapter);
            adapter.setmListener(this);
        } else {
            adapter.setList(list);
        }
    }

    @Override
    public void delHistory(String value) {
        String history = mSharePreference.getString(SEARCH_HISTORY, "");
        List<History> list = gson.fromJson(history, new TypeToken<List<History>>() {
        }.getType());
        for (int i = 0; i < list.size(); i++) {
            if (value.equals(list.get(i).getName())) {
                list.remove(i);
                break;
            }
        }
        SharedPreferences.Editor myEditor = mSharePreference.edit();
        myEditor.putString(SEARCH_HISTORY, gson.toJson(handlerHistoryList(list)));
        myEditor.commit();
        updateData();

    }

    @Override
    public void onItemClick(String value) {
        save(value);
        intentToActivity(value);
    }

    void intentToActivity(String value) {
        Intent intent = new Intent(this, StockListActivity.class);
        intent.putExtra("keyword", value);
        startActivity(intent);
    }

    List<History> handlerHistoryList(List<History> list) {

        if (list.size() <= 6) {
            return list;
        } else {
            return list.subList(0, 6);
        }

    }

}
